package openrr.map;

import java.util.ArrayList;

import openrr.geom.Mesh;
import openrr.geom.TextureGroup;

public class SoilManager {
	private Mesh map;
	
	private ArrayList<TileSet> sets;
	private ArrayList<int[]> colours;
	private ArrayList<String> types;
	
	private static final int[][] westFacingWall = 			{{-1, 1, 1},	{0, 1, 1}, 	{-1, 1, 1}};
	private static final int[][] northFacingWall = 			{{-1, 0, -1}, 	{1, 1, 1}, 	{1, 1, 1}};
	private static final int[][] eastFacingWall = 			{{1, 1, -1}, 	{1, 1, 0}, 	{1, 1, -1}};
	private static final int[][] southFacingWall = 			{{1, 1, 1}, 	{1, 1, 1},	{-1, 0, -1}};
	private static final int[][] roof = 					{{1, 1, 1}, 	{1, 1, 1}, 	{1, 1, 1}};
	private static final int[][] southwestCorneredWall = 	{{-1, 1, -1}, 	{0, 1, 1}, 	{-1, 0, -1}};
	private static final int[][] northwestCorneredWall = 	{{-1, 0, -1}, 	{0, 1, 1}, 	{-1, 1, -1}};
	private static final int[][] northeastCorneredWall = 	{{-1, 0, -1}, 	{1, 1, 0}, 	{-1, 1, -1}};
	private static final int[][] southeastCorneredWall = 	{{-1, 1, -1}, 	{1, 1, 0}, 	{-1, 0, -1}};
	private static final int[][] southwestInvertedWall = 	{{-1, 1, -1}, 	{1, 1, 1}, 	{0, 1, -1}};
	private static final int[][] northwestInvertedWall =	{{0, 1, -1}, 	{1, 1, 1}, 	{-1, 1, -1}};
	private static final int[][] northeastInvertedWall = 	{{-1, 1, 0}, 	{1, 1, 1}, 	{-1, 1, -1}};
	private static final int[][] southeastInvertedWall = 	{{-1, 1, -1}, 	{1, 1, 1}, 	{-1, 1, 0}};
	private static final int[][] westIntersection = 		{{0, 1, 1}, 	{1, 1, 0}, 	{1, 1, -1}};
	private static final int[][] northIntersection =		{{1, 1, 0}, 	{1, 1, 1}, 	{-1, 0, 1}};
	private static final int[][] eastIntersection = 		{{-1, 1, 1}, 	{0, 1, 1}, 	{1, 1, 0}};
	private static final int[][] southIntersection = 		{{1, 0, -1}, 	{1, 1, 1},	{0, 1, 1}};
	private static final int[][] westInvertedIntersection = {{1, 1, -1}, 	{1, 1, 0}, 	{0, 1, 1}};
	private static final int[][] northInvertedIntersection ={{-1, 0, 1}, 	{1, 1, 1}, 	{1, 1, 0}};
	private static final int[][] eastInvertedIntersection = {{1, 1, 0}, 	{0, 1, 1}, 	{-1, 1, 1}};
	private static final int[][] southInvertedIntersection ={{0, 1, 1}, 	{1, 1, 1},	{1, 0, -1}};
	
	
	public SoilManager(Mesh map)
	{
		this.map = map;
		this.sets = new ArrayList<TileSet>();
		this.colours = new ArrayList<int[]>();
		this.types = new ArrayList<String>();
	}
	public void addTileSet(String type, int[] colour, TileSet set)
	{
		//System.out.println("adding a TileSet of type " + type + " with a colour (" + colour[0] + ", " + colour[1] + ", " + colour[2] + ") at index " + this.sets.size());
		this.sets.add(set);
		this.colours.add(colour);
		this.types.add(type);
		
	}
	public TileSet getTileSetByColour(int[] colour)
	{
		for(int i = 0; i < this.colours.size(); i++)
		{
			int[] col = this.colours.get(i);
			if((col[0] == colour[0]) && (col[1] == colour[1]) && (col[2] == colour[2]))
			{
				//System.out.println("Found texture for colour (" + colour[0] + ", " + colour[1] + ", " + colour[2] + ") at index " + i);
				return this.sets.get(i);
			}
		}
		System.out.println("failed to find texture for colour (" + colour[0] + ", " + colour[1] + ", " + colour[2] + ")");
		return null;
	}
	public String[] getWallByArea(int[][] area)
	{
		String[] output = new String[2];
		
		if(checkArrayEquality(roof, area))
		{
			output[0] = "roof";
			output[1] = "north";
			return output;
		} else if(checkArrayEquality(westFacingWall, area))
		{
			output[0] = "basic";
			output[1] = "north";
			return output;
		} else if(checkArrayEquality(northFacingWall, area))
		{
			output[0] = "basic";
			output[1] = "west";
			return output;
		} else if(checkArrayEquality(eastFacingWall, area))
		{
			output[0] = "basic";
			output[1] = "south";
			return output;
		} else if(checkArrayEquality(southFacingWall, area))
		{
			output[0] = "basic";
			output[1] = "east";
			return output;
		} else if(checkArrayEquality(southwestCorneredWall, area))
		{
			output[0] = "corner";
			output[1] = "east";
			return output;
		} else if(checkArrayEquality(southeastCorneredWall, area))
		{
			output[0] = "corner";
			output[1] = "south";
			return output;
		} else if(checkArrayEquality(northwestCorneredWall, area))
		{
			output[0] = "corner";
			output[1] = "north";
			return output;
		} else if(checkArrayEquality(northeastCorneredWall, area))
		{
			output[0] = "corner";
			output[1] = "west";
			return output;
		} else if(checkArrayEquality(southwestInvertedWall, area))
		{
			output[0] = "inverted";
			output[1] = "east";
			return output;
		} else if(checkArrayEquality(southeastInvertedWall, area))
		{
			output[0] = "inverted";
			output[1] = "south";
			return output;
		} else if(checkArrayEquality(northwestInvertedWall, area))
		{
			output[0] = "inverted";
			output[1] = "north";
			return output;
		} else if(checkArrayEquality(northeastInvertedWall, area))
		{
			output[0] = "inverted";
			output[1] = "west";
			return output;
		} else if(checkArrayEquality(westIntersection, area))
		{
			output[0] = "corner";
			output[1] = "west";
			return output;
		} else if(checkArrayEquality(eastIntersection, area))
		{
			output[0] = "corner";
			output[1] = "east";
			return output;
		} else if(checkArrayEquality(southIntersection, area))
		{
			output[0] = "corner";
			output[1] = "north";
			return output;
		} else if(checkArrayEquality(northIntersection, area))
		{
			output[0] = "corner";
			output[1] = "south";
			return output;
		} else if(checkArrayEquality(westInvertedIntersection, area))
		{
			output[0] = "corner";
			output[1] = "south";
			return output;
		} else if(checkArrayEquality(eastInvertedIntersection, area))
		{
			output[0] = "corner";
			output[1] = "north";
			return output;
		} else if(checkArrayEquality(southInvertedIntersection, area))
		{
			output[0] = "corner";
			output[1] = "east";
			return output;
		} else if(checkArrayEquality(northInvertedIntersection, area))
		{
			output[0] = "corner";
			output[1] = "west";
			return output;
		}
		System.out.println("could not find wall");
		System.out.println(area[0][0] + " " + area[0][1] + " " + area[0][2]);
		System.out.println(area[1][0] + " " + area[1][1] + " " + area[1][2]);
		System.out.println(area[2][0] + " " + area[2][1] + " " + area[2][2]);
		return output;
	}
	private boolean checkArrayEquality(int[][] array1, int[][] array2)
	{
		for(int i = 0; i < array1.length; i++)
		{
			for(int j = 0; j < array1[0].length; j++)
			{
				if(array1[i][j] != -1)
				{
					if(array1[i][j] != array2[i][j])
					{
						return false;
					}
				}
				
			}
		}
		return true;
	}
}
